package com.car.advertising.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.car.advertising.R;
import com.car.advertising.connection.Contact;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.db.UserInfoBiz;
import com.car.advertising.db.UserInfoEntiy;
import com.car.advertising.view.EditTextDeleteView;
import com.support.widget.JumpManager;
import com.support.widget.layout.BaseToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends BaseToolBarActivity {

    private EditTextDeleteView phoneNumberEdt;
    private EditTextDeleteView verificationCodeEdt;
    private TextView loginTv;
    private TextView forgetPasswordTv;
    private TextView registerTv;
    private String phoneNum, password;
    private UserInfoBiz userInfoBiz;

    @Override
    protected void initToolBar() {
        setTitleClickable(false);
        setSubtitleClickable(true);
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("登录");
        setToolBarTitleColor(R.color.black_bg);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_login);
        phoneNumberEdt = (EditTextDeleteView) findViewById(R.id.login_phonenumber_edt);
        verificationCodeEdt = (EditTextDeleteView) findViewById(R.id.login_verificationcode_edt);
        loginTv = (TextView) findViewById(R.id.login_tv);
        forgetPasswordTv = (TextView) findViewById(R.id.login_forget_password_tv);
        registerTv = (TextView) findViewById(R.id.login_register_tv);

        loginTv.setOnClickListener(this);
        registerTv.setOnClickListener(this);

        userInfoBiz = UserInfoBiz.getInstance(LoginActivity.this);
    }

    private void setLogin(String userName, String password) {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("userName", userName));
        params.add(new OkHttpClientManager.Param("password", password));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_010, new OkHttpClientManager.ResultCallback<UserInfoEntiy>() {
            @Override
            public void onError(String request) {
                ToastUtil.show(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT);
                LogUtil.i(request);
            }

            @Override
            public void onResponse(UserInfoEntiy resp) {
                ToastUtil.show(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT);
                Contact.saveToken(resp.getAcToken());
                userInfoBiz.saveMerchant(resp);
                JumpManager.gotoActivity(LoginActivity.this, MainHomeActivity.class, true);
                finish();
            }

            @Override
            public void onFinish() {

            }
        }, params, null);
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {
        switch (v.getId()) {
            case R.id.login_tv:
                phoneNum = phoneNumberEdt.getText().toString();
                password = verificationCodeEdt.getText().toString();
                if (TextUtils.isEmpty(phoneNum)) {
                    ToastUtil.show(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT);
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    ToastUtil.show(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT);
                    return;
                }
                setLogin(phoneNum, password);
                break;

            case R.id.login_register_tv:
                JumpManager.gotoActivity(LoginActivity.this, RegsiterActivity.class, true);
                break;

            default:
                break;

        }
    }
}
